package com.hwz.controller.user;

import com.hwz.entity.AddressBook;
import com.hwz.result.Result;
import com.hwz.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/3/19 21:23]
 */
@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService  addressBookService;

    /**
     * 用户新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result insertAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.insertAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 查询用户所有地址
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> addressBooks =  addressBookService.getaddressBookList();
        return Result.success(addressBooks);
    }

    @GetMapping("/default")
    public Result<AddressBook> getAddressBookByDefault(){
        return Result.success(addressBookService.getAddressBookByDefault());
    }

    @PutMapping
    public Result updateAddressBook(AddressBook addressBook){
        addressBookService.updateAddressBook(addressBook);
        return Result.success();
    }
    @DeleteMapping
    public Result deleteAddressBook(Long id){
        addressBookService.deleteAddressBook(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id){
        return Result.success(addressBookService.getAddressBookById(id));
    }

    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return Result.success();
    }
}
